#ifndef ROBOTAUDIOI_H
#define ROBOTAUDIOI_H

#include <QtCore>
#include <QtNetwork>
#include <QAudioInput>
#include <QMediaPlayer>
#include <QSerialPort>
#include "tmasranswer.h"
#include "RobotDef.h"
#include <Ice/Ice.h>
#include "libkdxf/sharedlib_kdxf.h"

class TmAudioWorker:public QObject{
    Q_OBJECT
public:
    TmAudioWorker();
signals:
    void sig_asr_over(int nConfidence, QString strResult);

public slots:
    void slot_Init();
    // 语音合成
    void slot_TtsPlayParams(QString strTxt, QString strSpeaker, int nSpeed, int nPitch);
    void slot_TtsPlay(QString strTxt);
    void slot_TtsPlayFile(QString strPath);
    void slot_TtsSetVolume(int nVal);
    void slot_TtsStop();
    //    bool slot_TtsIsPlaying();
    // 语音识别
    void slot_AsrBuildCmd(QString strBnf);
    void slot_AsrSetGramma(QString name);
    void slot_AsrStart(int nMaxDelayMs, bool bIsOffline, bool bIsChinese);
    void slot_AsrStop();
    void slot_asr_over_online(QString strResult);
    // 音乐
    void slot_MediaPlay(QString strPath);
    void slot_MediaSetVolume(int nVal);
    void slot_MediaStop();
    //        bool slot_MediaIsPlaying();

    //麦克风阵列
    void slot_MicReset();
    void slot_MicSet(int nVal);

    void slot_audio_data();
    // 持续不断的音频输入

public:
    QMediaPlayer *m_pMediaPlayer;
    QMediaPlayer *m_pTTSPlayer;
private:
    void _asr_get_result();
    void _audio_data_in(QByteArray& bArray);

    bool m_bIsOffline;
    bool m_bOnlineEnable;
    QString m_tts_speaker;
    int m_tts_speed;
    int m_tts_pitch;
    int m_tts_volume;
    int m_media_volume;
    QString m_strDirTTS;
    Sharedlib_kdxf* m_pAsrSDK;
    int m_pcm_val;
    QTime m_nTimeEnd, m_nTimeStart;
    FILE* m_pAudioPCM;
    QString m_strGramName;

    QAudioInput* m_pAudioInput;
    QIODevice* m_pAudioDeviceIn;

    QSerialPort* m_pSerial;
    bool m_bIsSerInit;
};

class MyTimerTask :public QObject, virtual public IceUtil::TimerTask
{
    Q_OBJECT
signals:
    void sig_mic_reset();
public:
    MyTimerTask(QObject *parent=0);

    void runTimerTask();
};
typedef IceUtil::Handle<MyTimerTask> MyTimerTaskPtr;

class TmAudioSender:public QObject{
    Q_OBJECT
public:
    TmAudioSender();
    void Init();
    // 语音合成
    void TtsPlayParams(QString strTxt, QString strSpeaker, int nSpeed, int nPitch);
    void TtsPlay(QString strTxt);
    void TtsPlayFile(QString strPath);
    void TtsSetVolume(int nVal);
    void TtsStop();
    bool TtsIsPlaying();
    // 语音识别
    void AsrBuild(QString strBnfText);
    void AsrSetGramma(QString name);
    void AsrStart(int nMaxDelayMs,bool bIsOffline, bool bIsChinese);
    void AsrStop();
    // 音乐
    void MediaPlay(QString strPath);
    void MediaSetVolume(int nVal);
    void MediaStop();
    bool MediaIsPlaying();
    //麦克风阵列
    void MicReset();
    void MicSet(int nVal);

    void AsrSetTimeOut(int nTimeDelayS);

    void setExit();

    bool bAnswerEnable;
    int m_nTime;
    int m_nCount;
public slots:
    void slot_asr_result(int nConfidence, QString strResult);

signals:
    void sig_Init();
    // 语音合成
    void sig_TtsPlayParams(QString strTxt, QString strSpeaker, int nSpeed, int nPitch);
    void sig_TtsPlay(QString strTxt);
    void sig_TtsPlayFile(QString strPath);
    void sig_TtsSetVolume(int nVal);
    void sig_TtsStop();
    //    bool sig_TtsIsPlaying();
    // 语音识别
    void sig_AsrBuildCmd(QString strCmdBnf);
    void sig_AsrSetGramma(QString name);
    void sig_AsrStart(int nMaxDelayMs,bool bIsOffline, bool bIsChinese);
    void sig_AsrStop();
    // 音乐
    void sig_MediaPlay(QString strPath);
    void sig_MediaSetVolume(int nVal);
    void sig_MediaStop();
    //    bool sig_MediaIsPlaying();
    //麦克风阵列
    void sig_MicReset();
    void sig_MicSet(int nVal);

    void sig_getAsrAnswer(bool bIsChinese,QString strResult);
private:
    TmAudioWorker* m_pAudioWorker;
    QThread* m_pAudioTh;
    QThread* m_pAsrTh;
    TMAsrAnswer* g_pAsrAnswer;
    bool m_bIsOnline;
    bool m_bIsChinese;

    MyTimerTaskPtr mytask;
    IceUtil::TimerPtr m_pTimer;
};

class RobotAudioI : public Robot::Audio
{
public:
    RobotAudioI();
    void Init();
    // Audio interface
public:
    void TtsPlayParams(const std::string &strTxt, const std::string &strSpeaker, Ice::Int nSpeed, Ice::Int nPitch, const Ice::Current &);
    void TtsPlay(const std::string &strTxt, const Ice::Current &);
    void TtsPlayFile(const std::string &strPath, const Ice::Current &);
    void TtsSetVolume(Ice::Int nVal, const Ice::Current &);
    void TtsStop(const Ice::Current &);
    bool TtsIsPlaying(const Ice::Current &);

    void AsrBuild(const std::string &strBnfText, const Ice::Current &);
    void AsrSetGramma(const std::string &name, const Ice::Current &);
    void AsrStart(Ice::Int nMaxDelayMs, bool bIsOffline, const IceUtil::Optional<bool> &bIsChinese, const Ice::Current &);
    void AsrStop(const Ice::Current &);
    void AsrAnswerEnable(bool bEnable, const Ice::Current &);
    void AsrSetTimeOut(Ice::Int nTimeDelayS, const Ice::Current &);

    void MediaPlay(const std::string & strPath, const Ice::Current &);
    void MediaSetVolume(Ice::Int nVal, const Ice::Current &);
    void MediaStop(const Ice::Current &);
    bool MediaIsPlaying(const Ice::Current &);

    void MicReset(const Ice::Current &);
    void MicSet(Ice::Int nVal, const Ice::Current &);

};
#endif // ROBOTAUDIOI_H
